package GUILayer;

import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

import CtrLayer.SaleCtr;
import ModelLayer.Employee;
import ModelLayer.PartSale;
import ModelLayer.Sale;

/**
 * SaleGUI.
 *  Creates a saleGUI.
 *  This class contains a selection of salemenus fx beer, cigarettes, food etc.
 *  also contains a saleslist that displays the products allready added to a sale.
 * 31.05.2012
 * @author Lindgren
 * @Version 0.1
 */
public class SaleGUI {


	private JPanel saleGUIPanel;
	private Employee employee;
	private Sale sale;
	private JTextArea salesList;
	private JTextField total;

	/**
	 * Create a saleGUIPanel.
	 */
	public SaleGUI(Employee employee) {
		this.employee = employee;
		initialize();
		System.out.println("SaleGUI initialized!");
	}

	/**
	 * Initializes a saleGUI
	 */
	public void initialize()
	{

		System.out.println("SaleGUI initializing");

		saleGUIPanel = new JPanel();
		saleGUIPanel.setToolTipText("saleGUIPanel");
		saleGUIPanel.setForeground(Color.BLACK);
		saleGUIPanel.setName(employee.getLogin());

		MainFrame.getSaleGUIPanels().put(employee.getLogin(), saleGUIPanel);
		System.out.println("SaleGUI saleGUIPanel mapped: "+MainFrame.getSaleGUIPanels().get(employee.getLogin()));
		saleGUIPanel.setLayout(new GridLayout(0, 1, 0, 0));

		Box horizontalBox = Box.createHorizontalBox();
		saleGUIPanel.add(horizontalBox);

		JPanel btnPanel = new JPanel();
		horizontalBox.add(btnPanel);
		btnPanel.setLayout(new GridLayout(0, 1, 0, 0));

		JPanel panel_1 = new JPanel();
		btnPanel.add(panel_1);
		panel_1.setLayout(new GridLayout(0, 3, 0, 0));

		JButton btnDraughtBeer = new JButton("Draught Beer");
		btnDraughtBeer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				openDraughtBeerGUI(sale);
			}
		});
		panel_1.add(btnDraughtBeer);

		JButton btnBottledBeer = new JButton("Bottled Beer");
		btnBottledBeer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				openBottledBeerGUI(sale);
			}
		});
		panel_1.add(btnBottledBeer);

		JButton btnAlcopops = new JButton("Alcopops");
		panel_1.add(btnAlcopops);

		JPanel panel_2 = new JPanel();
		btnPanel.add(panel_2);
		panel_2.setLayout(new GridLayout(0, 3, 0, 0));

		JButton btnDrinks = new JButton("Drinks");
		panel_2.add(btnDrinks);

		JButton btnShots = new JButton("Shots");
		panel_2.add(btnShots);

		JButton btnSpirits = new JButton("Spirits");
		panel_2.add(btnSpirits);

		JPanel panel_3 = new JPanel();
		btnPanel.add(panel_3);
		panel_3.setLayout(new GridLayout(0, 3, 0, 0));

		JButton btnWholeBottles = new JButton("Whole Bottles");
		panel_3.add(btnWholeBottles);

		JButton btnCigarettes = new JButton("Cigarettes");
		panel_3.add(btnCigarettes);

		JButton btnSnacks = new JButton("Snacks");
		panel_3.add(btnSnacks);

		JPanel panel_4 = new JPanel();
		btnPanel.add(panel_4);
		panel_4.setLayout(new GridLayout(0, 3, 0, 0));

		JButton btnSoda = new JButton("Soda");
		panel_4.add(btnSoda);

		JButton btnFood = new JButton("Food");
		panel_4.add(btnFood);

		JButton btnMerchandise = new JButton("Merchandise");
		panel_4.add(btnMerchandise);

		JPanel salesListPanelpanel = new JPanel();
		horizontalBox.add(salesListPanelpanel);
		salesListPanelpanel.setBorder(new TitledBorder(null, "Sales List", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		salesListPanelpanel.setLayout(new GridLayout(0, 1, 0, 0));

		salesList = new JTextArea();
		salesList.setEditable(false);
		salesListPanelpanel.add(salesList);

		JPanel panel_6 = new JPanel();
		saleGUIPanel.add(panel_6);
		panel_6.setLayout(new GridLayout(0, 1, 0, 0));

		JPanel panel_7 = new JPanel();
		panel_6.add(panel_7);
		panel_7.setLayout(null);

		total = new JTextField();
		total.setBackground(Color.WHITE);
		total.setEditable(false);
		total.setBounds(1113, 11, 125, 20);
		panel_7.add(total);
		total.setColumns(10);

		JLabel lblKr = new JLabel("Kr. ");
		lblKr.setBounds(1251, 14, 25, 14);
		panel_7.add(lblKr);

		JLabel lblTotal = new JLabel("Total:");
		lblTotal.setBounds(1057, 14, 46, 14);
		panel_7.add(lblTotal);

		Box horizontalBox_1 = Box.createHorizontalBox();
		panel_6.add(horizontalBox_1);

		JPanel panel = new JPanel();
		horizontalBox_1.add(panel);
		panel.setLayout(new GridLayout(0, 4, 0, 0));

		JButton btnBack = new JButton("Back");
		panel.add(btnBack);
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				back();
			}
		});
		btnBack.setForeground(Color.BLACK);
		btnBack.setBackground(Color.YELLOW);

		JButton btnMinus = new JButton("Minus");
		btnMinus.setForeground(Color.WHITE);
		btnMinus.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				minus();				
				updateSalesList();
			}
		});
		btnMinus.setBackground(Color.RED);
		panel.add(btnMinus);

		Component glue_1 = Box.createGlue();
		panel.add(glue_1);

		JPanel panel_5 = new JPanel();
		panel.add(panel_5);
		panel_5.setLayout(new GridLayout(0, 1, 0, 0));

		JButton btnGoToPayment = new JButton("Go To Payment");
		panel_5.add(btnGoToPayment);
		btnGoToPayment.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				endSale(sale);
				openPaymentGUI();
			}
		});
		btnGoToPayment.setForeground(Color.WHITE);
		btnGoToPayment.setBackground(Color.GREEN);

		JPanel panel_8 = new JPanel();
		panel_6.add(panel_8);

	}

	/**
	 * @return the saleGUIPanel
	 */
	public JPanel getSaleGUIPanel() {
		return saleGUIPanel;
	}

	/**
	 * @param saleGUIPanel the saleGUIPanel to set
	 */
	public void setSaleGUIPanel(JPanel saleGUIPanel) {
		this.saleGUIPanel = saleGUIPanel;
	}

	/**
	 * Opens the openDraughtBeerGUI.
	 */
	private void openDraughtBeerGUI(Sale sale)
	{
		try {
			MainFrame.getUserFramePanels().get(employee.getLogin()).remove(MainFrame.getSaleGUIPanels().get(employee.getLogin()));
			MainFrame.getUserFramePanels().get(employee.getLogin()).add(MainFrame.getDraughtBeerGUIPanels().get(employee.getLogin()));
			MainFrame.getDraughtBeerGUIPanels().get(employee.getLogin()).repaint();
			MainFrame.getDraughtBeerGUIs().get(employee.getLogin()).updateSalesList();
		} catch (Exception e) {
			System.err.println("openning draughtBeerGUI failed");
			e.printStackTrace();
		}
	}

	/**
	 * Opens the openBottledBeerGUI.
	 */
	private void openBottledBeerGUI(Sale sale)
	{
		try {
			MainFrame.getUserFramePanels().get(employee.getLogin()).remove(MainFrame.getSaleGUIPanels().get(employee.getLogin()));
			MainFrame.getUserFramePanels().get(employee.getLogin()).add(MainFrame.getBottledBeerGUIPanels().get(employee.getLogin()));
			MainFrame.getBottledBeerGUIPanels().get(employee.getLogin()).repaint();
			MainFrame.getBottledBeerGUIs().get(employee.getLogin()).updateSalesList();
		} catch (Exception e) {
			System.err.println("opening bottledBeerGUI failed");
			e.printStackTrace();
		}
	}

	/**
	 * Opens the paymentGUI.
	 */
	private void openPaymentGUI()
	{
		try {
			MainFrame.getUserFramePanels().get(employee.getLogin()).remove(MainFrame.getSaleGUIPanels().get(employee.getLogin()));
			MainFrame.getUserFramePanels().get(employee.getLogin()).add(MainFrame.getPaymentGUIPanels().get(employee.getLogin()));
			MainFrame.getPaymentGUIPanels().get(employee.getLogin()).repaint();
			MainFrame.getPaymentGUIs().get(employee.getLogin()).updateSalesList();
		} catch (Exception e) {
			System.err.println("opening paymentGUI failed");
			e.printStackTrace();
		}
	}

	/**
	 * Calculates the total price
	 */
	private void endSale(Sale sale)
	{
		try {
			SaleCtr saleCtr = SaleCtr.getInstance();
			saleCtr.endSale(sale);
		} catch (Exception e) {
			System.err.println("Calculating total price failed");
			e.printStackTrace();
		}
	}
	public void setSale(Sale sale)
	{
		this.sale = sale;
	}

	/**
	 * @return the sale
	 */
	public Sale getSale() {
		return sale;
	}

	/**
	 * Updates the salesList
	 */
	public void updateSalesList()
	{
		try {
			salesList.setText("");
			Sale sale = MainFrame.getSaleGUIs().get(employee.getLogin()).getSale();
			ArrayList<PartSale> partSales = sale.getPartsales();
			for(PartSale partSale : partSales)
			{
				salesList.insert((" Product: " + partSale.getProduct().getName() + " amount: " + partSale.getAmount() + " Price: " + partSale.getPrice() + " kr. \n"), 0);
			}
			SaleCtr saleCtr = SaleCtr.getInstance();
			saleCtr.endSale(sale);
			total.setText(String.valueOf(sale.getTotal()));
		} catch (Exception e) {
			System.err.println("updating salesList failde");
			e.printStackTrace();
		}
	}

	/**
	 * @return the salesList
	 */
	public JTextArea getSalesList() {
		return salesList;
	}

	/**
	 * Returns the user to his mainGUI.
	 */
	private void back()
	{
		try {
			MainFrame.getUserFramePanels().get(employee.getLogin()).remove(MainFrame.getSaleGUIPanels().get(employee.getLogin()));
			MainFrame.getUserFramePanels().get(employee.getLogin()).add(MainFrame.getMainGUIPanels().get(employee.getLogin()));
			MainFrame.getMainGUIPanels().get(employee.getLogin()).repaint();
		} catch (Exception e) {
			System.err.println("Return to mainGUI failed");
			e.printStackTrace();
		}
	}

	/**
	 * Removes the last partSale from the sale 
	 */
	private void minus()
	{
		try {
			SaleCtr.getInstance().removePartSale( MainFrame.getSaleGUIs().get(employee.getLogin()).getSale() );
		} catch (Exception e) {
			System.err.println("removing partsale failed");
			e.printStackTrace();
		}
	}
}
